package benny.ApkposStore.db;

import benny.ApkposStore.model.SubDish;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

public class OrderTableSubDishs {
	/* table name */
	public static final String TABLENAME = "apkpos_orders_subdishs";
	public static final String TPrintNAME = "apkpos_subdishs_print";
	/* column names */
	static final public String CID = "_id"; // AUTO generate
	static final public String CTS = "cts"; // timestamp

	static final public String CORDERID = "orderid"; // order_id
	static final public String DISH_ID = "dish_id"; // inner_rank_id
	static final public String SUBDISH_ID = "subdish_id"; // sub_inner_rank_id
	static final public String DNBR_VAR = "dnbr_var"; // dish_id
	static final public String DNAM_VAR = "dnam_var"; // dish_name
	public static final String SUBDISH_NUM = "subdish_num"; // dish_count
	static final public String SUBDISH_PRICE = "subdish_price"; // dish_price
	public static final String SUBDISH_UNIT = "subdish_unit"; // dish_unit
	public static final String UNIT_ID = "unit_id"; // unit_inner_rank_id
	public static final String ORDER_STATE = "order_state";// is_cook
	public static final String ORDER_MODE = "order_mode"; // order_mode
	public static final String DCY_NAME = "dcy_name"; // order_man
	public static final String CZY_NAME = "czy_name"; // operator
	public static final String COOK_CONTENT = "cook_content"; // cook_content
	public static final String COOK_EXTRA = "cook_extra"; // cook_ectra_charge
	public static final String DISH_TIME = "dish_time"; // order_time
	public static final String DIC_RATE = "dic_rate"; // discount_rate
	public static final String SVC_STATE = "svc_state"; // is_service_charge
	public static final String ORIGINAL_PRICE = "original_price"; // original_price
	public static final String LBXH = "lbxh";
	public static final String LBNAME = "lbname"; // stat_class
	public static final String PDET_VAR = "pdet_var";
	public static final String PDET_VAR_IP = "pdet_var_ip";
	public static final String IS_PRINT = "is_print";//是否打单
	public static final String PRINT_LIST = "print_list";//总单已打列表

	// private String dish_time; //order_time
	//
	// private int dic_rate; //discount_rate
	// private int svc_state; //is_service_charge
	// private int original_price; //original_price
	// private int lbxh; //sta_class_id
	// private String lbname; //sta_class_name
	// private String pdet_var; //printer_name
	
	public static final Integer MYLOCK = 0;

	private Context mCtx;

	public static final String createTableSQL = "create table if not exists "
			+ TABLENAME + "(" + CID + " integer primary key autoincrement,"
			+ CTS + " int not null default 0," + CORDERID
			+ " varchar(40)  not null default 0," + DISH_ID
			+ " int not null default 0," + SUBDISH_ID
			+ " int not null default 0," + DNBR_VAR
			+ " int not null default 0," + DNAM_VAR
			+ " varchar(50) not null default 0," + SUBDISH_NUM
			+ " numeric(18,2) not null default 0," + SUBDISH_PRICE
			+ " numeric(18,2) not null default 0," + SUBDISH_UNIT
			+ " varchar(10) ," + UNIT_ID + " int ," + ORDER_STATE + "  int ,"
			+ ORDER_MODE + " varchar(10) ," + DCY_NAME + " varchar(15) ,"
			+ CZY_NAME + " varchar(15)," + COOK_CONTENT + " varchar(200) ,"
			+ DISH_TIME + " varchar(20)," + DIC_RATE + " int," + SVC_STATE
			+ " int," + ORIGINAL_PRICE + " int," + LBXH + " int," + LBNAME
			+ " varchar(10)," + PDET_VAR + " varchar(15)," + PDET_VAR_IP
			+ " varchar(20)," + COOK_EXTRA + " numeric(18,2),"
			+ IS_PRINT + " int," + PRINT_LIST + " varchar(100))";

	public static final String createPrintSQL = "create table if not exists "
			+ TPrintNAME + "(" + CID + " integer primary key autoincrement,"
			+ CTS + " int not null default 0," + CORDERID
			+ " varchar(40)  not null default 0," + DISH_ID
			+ " int not null default 0," + SUBDISH_ID
			+ " int not null default 0," + DNBR_VAR
			+ " int not null default 0," + DNAM_VAR
			+ " varchar(50) not null default 0," + SUBDISH_NUM
			+ " numeric(18,2) not null default 0," + SUBDISH_PRICE
			+ " numeric(18,2) not null default 0," + SUBDISH_UNIT
			+ " varchar(10) ," + UNIT_ID + " int ," + ORDER_STATE + "  int ,"
			+ ORDER_MODE + " varchar(10) ," + DCY_NAME + " varchar(15) ,"
			+ CZY_NAME + " varchar(15)," + COOK_CONTENT + " varchar(200) ,"
			+ DISH_TIME + " varchar(20)," + DIC_RATE + " int," + SVC_STATE
			+ " int," + ORIGINAL_PRICE + " int," + LBXH + " int," + LBNAME
			+ " varchar(10)," + PDET_VAR + " varchar(15)," + PDET_VAR_IP
			+ " varchar(20)," + COOK_EXTRA + " numeric(18,2),"
			+ IS_PRINT + " int," + PRINT_LIST + " varchar(100))";

	private static String tag = "apkpos_orders_subdishs";

	public OrderTableSubDishs(Context ctx) {
		mCtx = ctx;
	}

	/**
	 * insert a row
	 * 
	 * @param bean
	 *            :
	 * @param context
	 *            :Application's context
	 * @return the row ID of the newly inserted row, or -1 if an error occurred
	 */
	public long insert(SubDish bean) {
		synchronized(OrderTableSubDishs.MYLOCK) {
			ContentValues values = beanToContentValues(bean);
			values.remove(CID);
	
			try {
				SQLiteDatabase db = DBHelper.getDatabase(mCtx,1);
				long rowId = db.insertOrThrow(TABLENAME, null, values);
				Log.d(tag, "insert a row success");
				return rowId;
			} catch (SQLiteException e) {
				Log.e(tag, "insert " + TABLENAME + " error:" + e.getMessage());
			}
	
			return -1;
		}
	}

	/**
	 * get a table cursor
	 * 
	 * @param ctx
	 *            :Application's context
	 * @return cursor
	 */
	public Cursor getCurSor(String selection, String selectionArgs[]) {
		String columns[] = { CID, CTS, CORDERID, DISH_ID, DNBR_VAR,
				SUBDISH_NUM, SUBDISH_PRICE, SUBDISH_UNIT, UNIT_ID, ORDER_STATE,
				ORDER_MODE, DCY_NAME, CZY_NAME, COOK_CONTENT, DISH_TIME,
				SVC_STATE, ORIGINAL_PRICE, LBXH, LBNAME, PDET_VAR, COOK_EXTRA

		};
		// String selection = CORDERID +"= ?";
		// String selectionArgs[] = {"100"};
		String groupBy = "";
		String having = "";
		String orderBy = "";
		String limit = "";

		Cursor cursor = null;
		try {
			SQLiteDatabase db = DBHelper.getDatabase(mCtx,1);
			cursor = db.query(TABLENAME, columns, selection, selectionArgs,
					groupBy, having, orderBy, limit);
			return cursor;
		} catch (SQLiteException e) {
			Log.e(tag, "query failed:" + e.getMessage());
		}
		return null;
	}

	/**
	 * update a service record
	 * 
	 * @param bean
	 */
	public boolean update(SubDish bean) {
		synchronized(OrderTableSubDishs.MYLOCK) {
			try {
				String whereClause = " _id= " + bean.getId();
				SQLiteDatabase db = DBHelper.getDatabase(mCtx,1);
				ContentValues values = beanToContentValues(bean);
				db.update(TABLENAME, values, whereClause, null);
				Log.d(tag, "update a row success");
			} catch (SQLiteException e) {
				Log.e(tag, "update " + TABLENAME + " error:" + e.getMessage());
				return false;
			}
			return true;
		}
	}

	/**
	 * delete a row
	 * 
	 * @param rowId
	 * @param ctx
	 * @return
	 */
	public boolean delete(int rowId) {
		synchronized(OrderTableSubDishs.MYLOCK) {
			try {
				SQLiteDatabase db = DBHelper.getDatabase(mCtx,1);
				db.execSQL("delete from " + TABLENAME + " where _id = " + rowId);
				Log.d(tag, "delete a row success");
			} catch (SQLiteException e) {
				Log.e(tag, "delete failed:" + e.getMessage());
				return false;
			}
			return true;
		}
	}

	/**
	 * 按订单号删除
	 * 
	 * @param orderid
	 * @return
	 */
	public boolean deleteByOrderid(String orderid) {
		synchronized(OrderTableSubDishs.MYLOCK) {
			try {
				SQLiteDatabase db = DBHelper.getDatabase(mCtx,1);
				db.execSQL("delete from " + TABLENAME + " where " + CORDERID
						+ " = " + orderid);
				Log.d(tag, "delete a row success");
			} catch (SQLiteException e) {
				Log.e(tag, "delete failed:" + e.getMessage());
				return false;
			}
			return true;
		}
	}

	/**
	 * convert db cursor to bean structure
	 * 
	 * @param cursor
	 * @return Order
	 */
	public SubDish CurSorToGasBean(Cursor cursor) {
		SubDish bean = new SubDish();
		bean.setId(cursor.getInt(cursor.getColumnIndex(CID)));
		bean.setTs(cursor.getInt(cursor.getColumnIndex(CTS)));
		bean.setOrderid(cursor.getString(cursor.getColumnIndex(CORDERID)));
		bean.setDish_id(cursor.getInt(cursor.getColumnIndex(DISH_ID)));
		bean.setSubdish_id(cursor.getInt(cursor.getColumnIndex(SUBDISH_ID)));
		bean.setDnbr_var(cursor.getInt(cursor.getColumnIndex(DNBR_VAR)));
		bean.setSubdish_num(cursor.getInt(cursor.getColumnIndex(SUBDISH_NUM)));
		bean.setSubdish_price(cursor.getInt(cursor
				.getColumnIndex(SUBDISH_PRICE)));
		bean.setSubdish_unit(cursor.getString(cursor
				.getColumnIndex(SUBDISH_UNIT)));
		bean.setUnit_id(cursor.getInt(cursor.getColumnIndex(UNIT_ID)));
		bean.setOrder_state(cursor.getInt(cursor.getColumnIndex(ORDER_STATE)));
		bean.setOrder_mode(cursor.getString(cursor.getColumnIndex(ORDER_MODE)));
		bean.setDcy_name(cursor.getString(cursor.getColumnIndex(DCY_NAME)));
		bean.setCzy_name(cursor.getString(cursor.getColumnIndex(CZY_NAME)));
		bean.setCook_content(cursor.getString(cursor
				.getColumnIndex(COOK_CONTENT)));
		bean.setCook_extra(cursor.getInt(cursor.getColumnIndex(COOK_EXTRA)));
		bean.setDish_time(cursor.getString(cursor.getColumnIndex(DISH_TIME)));
		bean.setDic_rate(cursor.getInt(cursor.getColumnIndex(DIC_RATE)));
		bean.setSvc_state(cursor.getInt(cursor.getColumnIndex(SVC_STATE)));
		bean.setLbxh(cursor.getInt(cursor.getColumnIndex(LBXH)));
		bean.setLbname(cursor.getString(cursor.getColumnIndex(LBNAME)));
		bean.setPdet_var(cursor.getString(cursor.getColumnIndex(PDET_VAR)));

		return bean;
	}

	private ContentValues beanToContentValues(SubDish bean) {
		ContentValues values = new ContentValues();

		values.put(CID, bean.getId());
		values.put(CTS, bean.getTs());
		values.put(CORDERID, bean.getOrderid());
		values.put(DISH_ID, bean.getDish_id());
		values.put(SUBDISH_ID, bean.getDish_id());
		values.put(DNBR_VAR, bean.getDnbr_var());
		values.put(SUBDISH_NUM, bean.getSubdish_num());
		values.put(SUBDISH_PRICE, bean.getSubdish_price());
		values.put(SUBDISH_UNIT, bean.getSubdish_unit());
		values.put(UNIT_ID, bean.getUnit_id());
		values.put(ORDER_STATE, bean.getOrder_state());
		values.put(ORDER_MODE, bean.getOrder_mode());
		values.put(DCY_NAME, bean.getDcy_name());
		values.put(CZY_NAME, bean.getCzy_name());
		values.put(COOK_CONTENT, bean.getCook_content());
		values.put(COOK_EXTRA, bean.getCook_extra());
		values.put(DISH_TIME, bean.getDish_time());
		values.put(DIC_RATE, bean.getDic_rate());
		values.put(SVC_STATE, bean.getSvc_state());
		values.put(ORIGINAL_PRICE, bean.getOriginal_price());
		values.put(LBXH, bean.getLbxh());
		values.put(LBNAME, bean.getLbname());
		values.put(PDET_VAR, bean.getPdet_var());

		return values;
	}

}
